package cn.lijigou.shopping;

import cn.lijigou.shopping.service.LocationService;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.service.MobileNetworkListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.ReportPolicy;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainTabActivity extends TabActivity implements
		OnCheckedChangeListener, OnClickListener {

	public TabHost mHost;
	private Intent mShoppings;
	private Intent mOrders;
	private Intent mSettings;
	private Intent mHotbuy;
	private Intent mTodayBuy;
	// private Intent Shoppings;
	RadioButton main_tab_home = null;
	private RadioButton main_tab_order = null;
	private RadioButton main_tab_settings = null;
	public static int NOTIFY_NO_DATA = 101;
	public static int DOWN_SHOP_DATE = 102;
	private RadioButton main_tab_today = null;
	private RadioButton main_tab_hotbuy = null;
	public List<RadioButton> radioButtons;
	static Context mContext;
	static int i = 0;

	Animation home_push_rightout;
	Animation home_push_rightin;
	Animation home_push_leftout;
	Animation home_push_leftin;
	Map<String, Integer> map = new HashMap<String, Integer>();
	BroadcastReceiver mobileNetworkListener = null;
	public static Boolean firstReFresh = false;
	public static Handler showHander = new Handler() {
		
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == NOTIFY_NO_DATA) {
				if (mContext != null) {
					Toast.makeText(mContext, "亲，网络存在问题哦..", 1).show();
				}
			} else if (msg.what == DOWN_SHOP_DATE) {
				Toast.makeText(mContext, "亲,已经是最后一条了哦", 1).show();
			}
		}

	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tab);
		overridePendingTransition(R.anim.push_in, R.anim.push_out);

		Intent serviceIntent = this.getIntent();

		mobileNetworkListener = new MobileNetworkListener();
		if (mobileNetworkListener != null) {
			IntentFilter intent = new IntentFilter();
			intent.addAction("android.net.conn.CONNECTIVITY_CHANGE");
			this.registerReceiver(mobileNetworkListener, intent);
		}
		this.mShoppings = new Intent(this, TodayBuyActivity.class);
		this.mTodayBuy = new Intent(this, HomeActivity.class);
		this.mHotbuy = new Intent(this, BuySiteActivity.class);
		this.mOrders = new Intent(this, OrdersActivity.class);
		this.mSettings = new Intent(this, SettingsActivity.class);
		home_push_rightout = AnimationUtils.loadAnimation(MainTabActivity.this,
				R.anim.home_push_rightout);
		home_push_rightin = AnimationUtils.loadAnimation(MainTabActivity.this,
				R.anim.home_push_rightin);
		home_push_leftout = AnimationUtils.loadAnimation(MainTabActivity.this,
				R.anim.home_push_leftout);
		home_push_leftin = AnimationUtils.loadAnimation(MainTabActivity.this,
				R.anim.home_push_leftin);

		mContext = this;
		initRadios();
		setupIntent();
		MobclickAgent.setDebugMode(true);
		MobclickAgent.setSessionContinueMillis(1000 * 60 * 60 * 12);
		MobclickAgent.onError(this);
		MobclickAgent.setAutoLocation(false);
		MobclickAgent
				.setDefaultReportPolicy(this, ReportPolicy.BATCH_AT_LAUNCH);
		MobclickAgent.updateOnlineConfig(this);

		if (this.getIntent().hasExtra("pushType")) {
			HomeActivity.dataType = 12;
			if (HomeActivity.dataSource.size() > 5) {
				HomeActivity.dataSource.get(HomeActivity.dataType).clear();
			}
			HomeActivity.dataPage[HomeActivity.dataType] = 1;
			HomeActivity.isload[HomeActivity.dataType] = true;
			HomeActivity.tempdataType = this.getIntent().getIntExtra(
					"pushType", 0);
			mHost.setCurrentTabByTag("Shoppings_tab");
		} else if (this.getIntent().hasExtra("order")) {
			HomeActivity.parentMainTab.mHost.setCurrentTabByTag("order_tab");
			HomeActivity.parentMainTab.radioButtons.get(3).setChecked(true);
		}

	}

	private void initRadios() {
		radioButtons = new ArrayList<RadioButton>();
		main_tab_home = ((RadioButton) findViewById(R.id.main_tab_home));
		main_tab_order = ((RadioButton) findViewById(R.id.main_tab_order));
		main_tab_settings = ((RadioButton) findViewById(R.id.main_tab_settings));
		main_tab_today = ((RadioButton) findViewById(R.id.main_tab_today));
		main_tab_hotbuy = ((RadioButton) findViewById(R.id.main_tab_hotbuy));
		radioButtons.add(main_tab_home);
		radioButtons.add(main_tab_today);
		radioButtons.add(main_tab_hotbuy);
		radioButtons.add(main_tab_order);
		radioButtons.add(main_tab_settings);

		main_tab_home.setOnCheckedChangeListener(this);
		main_tab_home.setOnClickListener(this);

		main_tab_order.setOnCheckedChangeListener(this);
		main_tab_settings.setOnCheckedChangeListener(this);
		main_tab_today.setOnCheckedChangeListener(this);
		main_tab_hotbuy.setOnCheckedChangeListener(this);
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		if (HomeActivity.search_view != null) {
			HomeActivity.search_view.setVisibility(View.GONE);
		}

		if (isChecked) {
			Boolean temp = false;
			switch (buttonView.getId()) {
			case R.id.main_tab_home:

				mHost.getCurrentView().startAnimation(home_push_rightout);
				this.mHost.setCurrentTabByTag("home_tab");
				mHost.getCurrentView().startAnimation(home_push_rightin);
				break;
			case R.id.main_tab_today:
				if (map.get("today_tab") < map.get(mHost.getCurrentTabTag())) {
					temp = true;
					mHost.getCurrentView().startAnimation(home_push_rightout);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftout);
				}

				this.mHost.setCurrentTabByTag("today_tab");

				if (temp) {
					mHost.getCurrentView().startAnimation(home_push_rightin);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftin);
				}
				break;
			case R.id.main_tab_hotbuy:
				if (map.get("hotbuy_tab") < map.get(mHost.getCurrentTabTag())) {
					temp = true;
					mHost.getCurrentView().startAnimation(home_push_rightout);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftout);
				}

				this.mHost.setCurrentTabByTag("hotbuy_tab");
				if (temp) {
					mHost.getCurrentView().startAnimation(home_push_rightin);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftin);
				}
				break;
			case R.id.main_tab_order:

				if (CompleteOrderActivity.isAnima) {
					CompleteOrderActivity.isAnima = true;
				} else {
					if (map.get("order_tab") < map
							.get(mHost.getCurrentTabTag())) {
						temp = true;
						mHost.getCurrentView().startAnimation(
								home_push_rightout);
					} else {
						mHost.getCurrentView()
								.startAnimation(home_push_leftout);
					}
				}
				this.mHost.setCurrentTabByTag("order_tab");
				if (!CompleteOrderActivity.isAnima) {
					if (temp) {
						mHost.getCurrentView()
								.startAnimation(home_push_rightin);
					} else {
						mHost.getCurrentView().startAnimation(home_push_leftin);
					}
				}
				break;
			case R.id.main_tab_settings:
				if (map.get("settings_tab") < map.get(mHost.getCurrentTabTag())) {
					temp = true;
					mHost.getCurrentView().startAnimation(home_push_rightout);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftout);
				}

				this.mHost.setCurrentTabByTag("settings_tab");
				if (temp) {
					mHost.getCurrentView().startAnimation(home_push_rightin);
				} else {
					mHost.getCurrentView().startAnimation(home_push_leftin);
				}
				break;

			}
		}
	}

	private void setupIntent() {
		this.mHost = getTabHost();

		mHost.addTab(buildTabSpec("home_tab", "11111", R.drawable.tab_home,
				mShoppings));

		mHost.addTab(buildTabSpec("today_tab", "2222", R.drawable.tab_apps,
				mTodayBuy));

		mHost.addTab(buildTabSpec("hotbuy_tab", "3333",
				R.drawable.tab_settings, mHotbuy));

		mHost.addTab(buildTabSpec("order_tab", "4444", R.drawable.tab_order,
				mOrders));

		mHost.addTab(buildTabSpec("settings_tab", "5555",
				R.drawable.tab_settings, mSettings));

		map.put("home_tab", 1);
		map.put("today_tab", 2);
		map.put("hotbuy_tab", 3);
		map.put("order_tab", 4);
		map.put("settings_tab", 5);
		map.put("Shoppings_tab", 6);

		// mHost.addTab(buildTabSpec("today_tab", "44444",
		// R.drawable.tab_apps, mApps));

		// mHost.addTab(buildTabSpec("Shoppings_tab", "55555",
		// R.drawable.tab_apps, Shoppings));

	}

	private TabHost.TabSpec buildTabSpec(String tag, String resLabel,
			int resIcon, final Intent content) {
		return this.mHost.newTabSpec(tag).setIndicator(resLabel,
				getResources().getDrawable(resIcon)).setContent(content);
	}

	
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mobileNetworkListener != null) {
			this.unregisterReceiver(mobileNetworkListener);
			mobileNetworkListener = null;
		}
		super.onDestroy();
	}

	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// this.mHost.setCurrentTabByTag("home_tab");
		if (HomeActivity.search_view != null) {
			HomeActivity.search_view.setVisibility(View.GONE);
		}

	}

	
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);

	}

	
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

}
